﻿using System;
using System.Net;
using System.Collections;
using System.Management;
using System.Collections.Generic;

namespace Collector
{
    public class MachineInfoCollecter
    {
        private ManagementObjectCollection Search(String key)
        {
            return new ManagementObjectSearcher("select * from " + key).Get();
        }

        private List<T> Search<T>(String key, String prop)
        {
            List<T> propVals = new List<T>();
            ManagementObjectCollection moc = Search(key);
            foreach (ManagementObject mo in moc)
            {
                if (mo.GetPropertyValue(prop) != null)
                {
                    propVals.Add((T)mo.GetPropertyValue(prop));
                }
            }
            return propVals;
        }

        private Hashtable getPropty(ManagementObject mo, List<String> props)
        {
            Hashtable propVals = new Hashtable();
            foreach (String prop in props)
            {
                if (mo.GetPropertyValue(prop) != null)
                {
                    propVals.Add(prop,mo.GetPropertyValue(prop));
                }
            }
            return propVals;
        }

        private List<Hashtable> Search(String key, List<String> props)
        {
            List<Hashtable> propColl = new List<Hashtable>();
            ManagementObjectCollection moc = Search(key);
            foreach (ManagementObject mo in moc)
            {
                propColl.Add(getPropty(mo,props));
            }
            return propColl;
        }

        public string UserName()
        {
            return Environment.UserName;
        }
        public String HostName()
        {
            return Dns.GetHostName();
        }
        public List<String> HD_SN()
        {
            try
            {
                return Search<String>("Win32_PhysicalMedia", "SerialNumber");
            }
            catch
            {
                return null;
            }
        }
        public List<String> CPU_SN()
        {
            try
            {
                return Search<String>("Win32_Processor", "ProcessorId");
            }
            catch
            {
                return null;
            }            
        }
        public List<Hashtable> CPU_Infos()
        {
            try
            {
                Hashtable ret = new Hashtable();
                List<String> props = new List<string> { "ProcessorId","AddressWidth", "Caption", "DeviceID", "Name", "NumberOfCores", "NumberOfLogicalProcessors", "L2CacheSize"};
                return Search("Win32_Processor", props);
            }
            catch
            {
                return null;
            }
        }
        public List<Hashtable> HD_Infos()
        {
            try
            {
                Hashtable ret = new Hashtable();
                List<String> props = new List<string> { "InterfaceType", "Caption", "SerialNumber", "Name", "Size" };
                return Search("Win32_DiskDrive", props);
            }
            catch
            {
                return null;
            }
        }
        public List<String> MAC()
        {
            try
            {
                return Search<String>("Win32_NetworkAdapterConfiguration", "MacAddress");
            }
            catch
            {
                return null;
            }            
        }
    }
}
